import { createRouter, createWebHistory } from 'vue-router'
import routes from './Coustroutes'
import NProgress from 'nprogress'
import "nprogress/nprogress.css"

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})


router.beforeEach((to,from,next)=>{

  var token = localStorage.getItem('token');
  if( token ){ 
    if( to.path == '/login' ){ 
      next('/');
    }else{ 
      next();
    }
  }else{ 
    if( to.path == '/login'|| to.path == '/register'  ){ 
      next();
    }else{ 
      next('/login');
    }
  }  

  document.title = to.meta.title;

  NProgress.start();

})

router.afterEach((to,from)=>{
 
  NProgress.done();
})

export default router
